/*
 * @Author: heinan
 * @Date: 2020-07-16 11:33:19
 * @Last Modified by: yaozixian
 * @Last Modified time: 2023-08-09 20:09:07
 */
"use strict";
const {
  Service
} = require("egg");
const {
  idCreator
} = require("../utils/index");

class HouseService extends Service {
  async index({
    area,
    name,
    id,
    currentPage = 1,
    pageSize = 10,
    status,
    toword,
    house_type,
    price,
    sort = 'desc'
  }) {
    const where = {};
    if (area) {
      area = area.replace(/(区|县)/, "");
      where.area = area;
    }
    if (name) {
      where.name = name;
    }
    if (id) {
      where.id = id;
    }
    if (toword) {
      toword = toword === '不限' ? "" : toword;
      where.toward = toword
    }
    if (house_type) {
      house_type = house_type === '不限' ? "" : house_type;
      where.house_type = house_type
    }
    if (price) {
      price = price ? JSON.parse(price) : {};
      if ((price.min || price.min === 0) && price.max) {
        where.price = [price.min, price.max]
      } else if (price.min && !price.max) {
        where.price = [0, price.min]
      } else if (price.max && !price.min) {
        // 小于
        where.price = [price.max, 1000000000]
      }
    }
    if (status == "全部") {
      status = "";
      where.status = status
    } else if (status) {
      where.status = status;
    } else {}
    const result = await this.app.mysql.select("renthouses", {
      where,
      orders: [
        ['price', sort]
      ],
      offset: (currentPage - 1) * pageSize,
      limit: parseInt(pageSize),
    });
    // console.log(result);
    const total = await this.app.mysql.count("renthouses", where);
    return {
      data: result,
      total,
      pageSize: Number(pageSize),
      currentPage: Number(currentPage),
    };
  }

  // 删除房源管理销售楼盘数据接口
  async destroy({
    id
  }) {
    return await this.app.mysql.delete("renthouses", {
      id,
    });
  }

  // 修改房源管理销售楼盘状态接口
  async update({
    id,
    status
  }) {
    const $data = {
      status,
    };
    const $options = {
      where: {
        id,
      },
    };
    return await this.app.mysql.update("renthouses", $data, $options);
  }

  // 修改房源经纪人数据接口
  async updateBroker({
    id,
    broker
  }) {
    const $data = {
      broker,
    };
    const $options = {
      where: {
        id,
      },
    };
    return await this.app.mysql.update("renthouses", $data, $options);
  }

  // 添加新房源
  async create({
    position,
    toward, //朝向
    subway, //subway
    lift, //电梯
    house_type, //房型
    way, //way
    img,
    price,
    broker,
    ll,
    building_type, //楼型
    section, //楼段
    extent, //面积
    name, //房名
    brand, //brand
    periods, //期数
    area, //区域
    feature, //特色
    resident, //小区
    useto, //用途
    pay, //支付
    lease, //租期
    renovation, //装修
    status,
  }) {

    const $data = {
      id: idCreator(),
      create_time: new Date().getTime(),
      update_time: new Date().getTime(),
      toward,
      subway,
      lift,
      house_type,
      way,
      img,
      price,
      broker,
      ll,
      building_type,
      section,
      extent,
      name,
      brand,
      periods,
      area,
      feature,
      resident,
      useto,
      pay,
      lease,
      renovation,
      status,
      position
    };
    console.log($data, '==================================>data');
    return await this.app.mysql.insert("renthouses", $data);
  }

  // 修改房源
  async editHouse({
    toward, //朝向
    subway, //subway
    lift, //电梯
    house_type, //房型
    way, //way
    img,
    price,
    broker,
    ll,
    building_type, //楼型
    section, //楼段
    extent, //面积
    name, //房名
    brand, //brand
    periods, //期数
    area, //区域
    feature, //特色
    resident, //小区
    useto, //用途
    pay, //支付
    lease, //租期
    renovation, //装修
    status,
    id,
    position
  }) {
    const $data = {
      update_time: new Date().getTime(),
      toward,
      subway,
      lift,
      house_type,
      way,
      img,
      price,
      broker,
      ll,
      building_type,
      section,
      extent,
      name,
      brand,
      periods,
      area,
      feature,
      resident,
      useto,
      pay,
      lease,
      renovation,
      status,
      position
    };
    const $options = {
      where: {
        id,
      },
    };
    return await this.app.mysql.update("renthouses", $data, $options);
  }

  // ------------二手房房源-----------
  async secondIndex({
    area,
    name,
    id,
    currentPage = 1,
    pageSize = 10,
    status,
    toword,
    house_type,
    price,
    sort = 'asc'
  }) {
    const where = {};
    if (area) {
      area = area.replace(/(区|县)/, "");
      where.area = area;
    }
    if (name) {
      where.name = name;
    }
    if (id) {
      where.id = id;
    }
    if (toword) {
      toword = toword === '不限' ? "" : toword;
      where.toward = toword
    }
    if (house_type) {
      house_type = house_type === '不限' ? "" : house_type;
      where.house_type = house_type
    }
    if (price) {
      price = price ? JSON.parse(price) : {};
      if ((price.min || price.min === 0) && price.max) {
        where.price = [price.min, price.max]
      } else if (price.min && !price.max) {
        where.price = [0, price.min]
      } else if (price.max && !price.min) {
        // 小于
        where.price = [price.max, 1000000000]
      }
    }
    if (status == "全部") {
      status = "";
    } else if (status) {
      where.status = status;
    }
    const result = await this.app.mysql.select("secondhouses", {
      where,
      orders: [
        ['price', sort]
      ],
      offset: (currentPage - 1) * pageSize,
      limit: parseInt(pageSize),

    });
    console.log(result);
    const total = await this.app.mysql.count("secondhouses", where);
    return {
      data: result,
      total,
      pageSize: Number(pageSize),
      currentPage: Number(currentPage),
    };
  }

  // 修改二手房源状态接口
  async secondUpdate({
    id,
    status
  }) {
    const $data = {
      status,
    };
    const $options = {
      where: {
        id,
      },
    };
    return await this.app.mysql.update("secondhouses", $data, $options);
  }

  // 删除二手房源管理销售楼盘数据接口
  async secondDestroy({
    id
  }) {
    return await this.app.mysql.delete("secondhouses", {
      id,
    });
  }

  // 修改二手房源经纪人数据接口
  async updatesecondBroker({
    id,
    broker
  }) {
    const $data = {
      broker,
    };
    const $options = {
      where: {
        id,
      },
    };
    return await this.app.mysql.update("secondhouses", $data, $options);
  }

  // 添加二手房源
  async secondCreate({
    toward, //朝向
    subway, //subway
    lift, //电梯
    house_type, //房型
    img,
    price,
    broker,
    ll,
    building_type, //楼型
    extent, //面积
    name, //房名
    periods, //期数
    area, //区域
    feature, //特色
    resident, //小区
    useto, //用途
    renovation, //装修
    status, //装修
  }) {
    const $data = {
      id: idCreator(),
      create_time: new Date().getTime(),
      update_time: new Date().getTime(),
      toward, //朝向
      subway, //subway
      lift, //电梯
      house_type, //房型
      img,
      price,
      broker,
      ll,
      building_type, //楼型
      extent, //面积
      name, //房名
      periods, //期数
      area, //区域
      feature, //特色
      resident, //小区
      useto, //用途
      renovation, //装修
      status,
    };
    return await this.app.mysql.insert("secondhouses", $data);
  }

  // 修改房源
  async editSecondHouses({
    toward, //朝向
    subway, //subway
    lift, //电梯
    house_type, //房型
    img,
    price,
    broker,
    ll,
    building_type, //楼型
    extent, //面积
    name, //房名
    periods, //期数
    area, //区域
    feature, //特色
    resident, //小区
    useto, //用途
    renovation, //装修
    status,
    id,
  }) {
    const $data = {
      update_time: new Date().getTime(),
      toward, //朝向
      subway, //subway
      lift, //电梯
      house_type, //房型
      img,
      price,
      broker,
      ll,
      building_type, //楼型
      extent, //面积
      name, //房名
      periods, //期数
      area, //区域
      feature, //特色
      resident, //小区
      useto, //用途
      renovation, //装修
      status,
    };
    const $options = {
      where: {
        id,
      },
    };
    return await this.app.mysql.update("secondhouses", $data, $options);
  }

  //---------- 租房------------

  async getRent({
    pay,
    area,
    price,
    currentPage = 1,
    pageSize = 10,
    resident,
    toword,
    house_type,
    sort = "asc"
  }) {
    const where = {};
    if (area) {
      // area = area.replace(/(区|县)/, "");
      area = area === "不限" ? "" : area;
      where.area = area;
    }
    if (pay) {
      pay = pay === "不限" ? "" : pay;
      where.pay = pay;
    }
    if (toword) {
      toword = toword === '不限' ? "" : toword;
      where.toward = toword
    }
    if (house_type) {
      house_type = house_type === '不限' ? "" : house_type;
      where.house_type = house_type
    }
    if (price) {
      price = price ? JSON.parse(price) : {};
      if ((price.min || price.min === 0) && price.max) {
        where.price = [Number(price.min), Number(price.max)]
      } else if (price.min && !price.max) {
        where.price = [0, Number(price.min)]
      } else if (price.max && !price.min) {
        // 小于
        where.price = [Number(price.max), 1000000000]
      }
    }
    if (resident) {
      where.resident = resident;
    }
    console.log(price);
    const result = await this.app.mysql.select("renthouses", {
      where,
      orders: [
        ['price', sort]
      ],
      offset: (currentPage - 1) * pageSize,
      limit: parseInt(pageSize),
    });
    const total = await this.app.mysql.count("renthouses", where);
    return {
      data: result,
      total,
      pageSize: Number(pageSize),
      currentPage: Number(currentPage),
    };
  }
}

module.exports = HouseService;
